//
//  PhoneRegisterViewController.m
//  XDCommonApp
//
//  Created by wanglong8889@126.com on 14-6-11.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "PhoneRegisterViewController.h"
#import "PhoneTwoViewController.h"

#define XZB   20
@interface PhoneRegisterViewController ()

@end

@implementation PhoneRegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text =@"注册";
    
    [self initPhoneRegisterView];
    
}



-(void)initPhoneRegisterView{
    
    mScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_STATUS_BAR_HEIGHT -UI_STATUS_BAR_HEIGHT)];
    mScrollView.contentSize=CGSizeMake(UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_STATUS_BAR_HEIGHT );
    mScrollView.scrollEnabled =YES;
    mScrollView.delegate = self;
    mScrollView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:mScrollView];
    
    
    
    UILabel *inputLabel = [[UILabel alloc] initWithFrame:CGRectMake(XZB-10,15, 200, 15)];
    inputLabel.text = @"填写个人信息，立即完成注册";
    inputLabel.font = [UIFont systemFontOfSize:15.0];
    inputLabel.backgroundColor = [UIColor clearColor];
    [mScrollView addSubview:inputLabel];
    
    UIImageView *phoneView = [[UIImageView alloc] initWithFrame:CGRectMake(XZB, 74, 281, 41)];
    [phoneView setImage:[UIImage imageNamed:@"input_kuag"]];
    phoneView.userInteractionEnabled = YES;
    
    [mScrollView addSubview:phoneView];
    
    userTf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5,271, 31)];
    userTf.borderStyle = UITextBorderStyleNone;
    userTf.backgroundColor =  [UIColor clearColor];
    userTf.placeholder = @"请输入手机号";
    userTf.returnKeyType = UIReturnKeyDone;
    userTf.textColor = [UIColor grayColor];
    
    
    userTf.delegate = self;
    userTf.font = [UIFont systemFontOfSize:15.0];
    userTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [phoneView addSubview:userTf];
    
    
    UIButton *getVerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    getVerBtn.frame = CGRectMake(XZB, phoneView.frame.origin.y+phoneView.frame.size.height+10, 281, 40);
    [getVerBtn setBackgroundImage:[UIImage imageNamed:@"launch_btn"] forState:UIControlStateNormal];
    [getVerBtn setBackgroundImage:[UIImage imageNamed:@"launch_btn"] forState:UIControlStateHighlighted];
    [getVerBtn addTarget:self action:@selector(getVerticalAction:) forControlEvents:UIControlEventTouchUpInside];
    [getVerBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getVerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    getVerBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    
    [mScrollView addSubview:getVerBtn];
    
    
    
    UIImageView *verticalView = [[UIImageView alloc] initWithFrame:CGRectMake(XZB, getVerBtn.frame.origin.y+getVerBtn.frame.size.height+10, 281, 41)];
    [verticalView setImage:[UIImage imageNamed:@"input_kuag"]];
    verticalView.userInteractionEnabled = YES;
    
    [mScrollView addSubview:verticalView];
    
    verticalTf = [[UITextField alloc] initWithFrame:userTf.frame];
    verticalTf.borderStyle = UITextBorderStyleNone;
    verticalTf.backgroundColor =  [UIColor clearColor];
    verticalTf.placeholder = @"请输入验证码";
    verticalTf.returnKeyType = UIReturnKeyDone;
    
    verticalTf.textColor = [UIColor grayColor];
    verticalTf.returnKeyType = UIReturnKeyDone;
    verticalTf.delegate = self;
    verticalTf.font = [UIFont systemFontOfSize:15.0];
    verticalTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [verticalView addSubview:verticalTf];
    
    
    
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake(XZB, verticalView.frame.origin.y+verticalView.frame.size.height+92, 281, 40);
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"launch_btn"] forState:UIControlStateNormal];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"launch_btn"] forState:UIControlStateHighlighted];
    [nextBtn addTarget:self action:@selector(goNextAction:) forControlEvents:UIControlEventTouchUpInside];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    nextBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
    
    [mScrollView addSubview:nextBtn];
    


}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if (!iPhone5) {
        [UIView animateWithDuration:.3 animations:^{
            mScrollView.contentOffset = CGPointMake(0, 30);
        }];
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    [UIView animateWithDuration:.3 animations:^{
        mScrollView.contentOffset = CGPointMake(0, 0);
    }];
    return YES;
}


//获取验证码
-(void)getVerticalAction:(UIButton *)btn{
    if (!IS_NOT_EMPTY(userTf.text)) {
        [XDTools showTips:@"手机号不能为空" toView:self.view];
    }else{
        NSDictionary *dic = @{@"mobilePhone": userTf.text};
        [self getVerticaParms:dic];
    }
}


- (void)getVerticaParms:(NSDictionary *)parms{
    
    __weak  ASIFormDataRequest *request = [XDTools postRequestWithDict:parms API:API_GETVERTICAL];
    
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        if([[tempDic objectForKey:@"result"]intValue] == 1){
           
            [XDTools showTips:@"验证码已发送至手机" toView:self.view];
            
        }else{
            [XDTools showTips:tempDic[@"msg"] toView:self.view];
            
        }
    }];
    
    [request setFailedBlock:^{
        
        [XDTools hideProgress:self.view];
        NSError *error = [request error];
        DDLOG(@"error=%@",error);
    }];
    
    [XDTools showProgress:self.view];
    [request startAsynchronous];
    
}

//下一步
-(void)goNextAction:(UIButton *)btn{
    if (!IS_NOT_EMPTY(userTf.text)||!IS_NOT_EMPTY(verticalTf.text)) {
        [XDTools showTips:@"信息不完整" toView:self.view];
    }else{
        NSDictionary *dic = @{@"mobilePhone": userTf.text,
                              @"regcode":verticalTf.text};
        [self getPhoneRegisterOneParms:dic];
    }
    PhoneTwoViewController *phoneVC = [[PhoneTwoViewController alloc] init];
    phoneVC.phone = userTf.text;
    [self.navigationController pushViewController:phoneVC animated:YES];
    
}


//注册第一步
- (void)getPhoneRegisterOneParms:(NSDictionary *)parms{
    
    __weak  ASIFormDataRequest *request = [XDTools postRequestWithDict:parms API:API_REGISTERTONE];
    
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        if([[tempDic objectForKey:@"result"]intValue] == 1){
            
            PhoneTwoViewController *phoneVC = [[PhoneTwoViewController alloc] init];
            phoneVC.phone = userTf.text;
            [self.navigationController pushViewController:phoneVC animated:YES];
            
        }else{
            [XDTools showTips:tempDic[@"msg"] toView:self.view];
            
        }
    }];
    
    [request setFailedBlock:^{
        
        [XDTools hideProgress:self.view];
        NSError *error = [request error];
        DDLOG(@"error=%@",error);
    }];
    
    [XDTools showProgress:self.view];
    [request startAsynchronous];
    
}




- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [userTf resignFirstResponder];
    [verticalTf resignFirstResponder];
}






- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
